package com.xiaode.controller.app.task;

import cn.hutool.core.bean.BeanUtil;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Redis;
import com.xiaode.common.constant.Const;
import com.xiaode.common.model.ExampleActivityTask;
import com.xiaode.common.model.KeyResultsTask;
import com.xiaode.common.model.LabourTask;
import com.xiaode.common.model.OrdinaryUserTask;
import com.xiaode.common.model.Student;
import com.xiaode.common.model.TaskLog;
import com.xiaode.common.model.TeacherStudent;
import com.xiaode.common.model.VocationTask;
import com.xiaode.exce.EnumError;
import java.time.LocalDateTime;
import java.util.List;

public class ExamineService {

    
    /**
     * 老师查询学生审核列表
     */
    public Ret findExamineStudentPage(Integer userId, Integer userType, Integer state, Integer page, Integer limit) throws Exception {        
        if (userType != Const.UserType.admin) {
            return Ret.fail().set("error", EnumError.ALLOW_ADMIN_EXAMINE.getObj());
        }
        //查询这个老师管理的学生
        List<Integer> accountIdList = Db.query("SELECT (SELECT accountId FROM student WHERE id = ts.studentId) accountId FROM teacher_student ts WHERE ts.teacherId = ? AND ts.state = 2", userId);
        if (accountIdList.size() == 0) {
        	return Ret.ok("examinePage", null);
        }
        //查询待审核的用户
        Page<Record> recordPage = Db.template("findExamineStudentPage", Kv.of("accountIdList", accountIdList).set("state", state)).paginate(page, limit);
        for (Record record : recordPage.getList()) {
            Integer integer = record.getInt("accountId");
            Record rec = Redis.use().hget(Const.user.AccountIdInfo, integer);
            Student o = Redis.use().get(Const.user.Student + rec.getInt("id"));
            
            TaskLog taskLog = TaskLog.dao.findFirst("SELECT * FROM task_log WHERE user_account_id = ? ORDER BY id DESC", integer);
            LocalDateTime addTime = taskLog.getAddTime();
            o.put("userType", Const.UserType.student);
            o.put("time", addTime);
            //移除不需要的属性
            o.remove("salt","lastLoginTime","isDele","version","state","isMainland","lastLoginTimestamp","parent","example");
            
            record.put("student", o);
        }
        return Ret.ok("examinePage", recordPage);
    }
    
    /**
     * 查询学生的任务详情列表
     */
    public Ret findTaskDetailPage(Integer myUserId, Integer myUserType, Integer state, Integer accountId, Integer page, Integer limit) throws Exception {
        if (myUserType != Const.UserType.admin) {
            return Ret.fail().set("error", EnumError.ALLOW_ADMIN_EXAMINE.getObj());
        }
        //判断是否是导师的学生
        Record rec = Redis.use().hget(Const.user.AccountIdInfo, accountId);
        TeacherStudent first = TeacherStudent.dao.findFirst("SELECT id FROM teacher_student WHERE teacherId = ? AND studentId = ?", myUserId, rec.getInt("id"));
        if (BeanUtil.isEmpty(first)) {
            return Ret.fail().set("error", EnumError.NOT_USER_STUDENT.getObj());
        }
        //先查询任务记录
        Page<TaskLog> taskLogPage = TaskLog.dao.paginate(page, limit, "SELECT *", " FROM task_log WHERE user_account_id = ? AND task_state = ? ORDER BY id DESC", accountId, state);
        if (taskLogPage.getTotalRow() == 0) {
            return Ret.ok().set("taskPage", null);
        }
        //获取任务详情
        for(TaskLog taskLog : taskLogPage.getList()) {
        	switch (taskLog.getTaskType()) {
	        	case Const.taskType.ORDINARY_USER_TASK_TYPE://普通任务
	        		OrdinaryUserTask ordinaryUserTask = OrdinaryUserTask.dao.findById(taskLog.getTaskId());
	        		taskLog.put("task", ordinaryUserTask);
	                break;
	            case Const.taskType.LABORU_TASK_TYPE://日常劳动
	            	LabourTask labourTask = LabourTask.dao.findById(taskLog.getTaskId());
	            	taskLog.put("task", labourTask);
	                break;
	            case Const.taskType.VOCATION_TASK_TYPE://职业体验
	            	VocationTask vocationTask = VocationTask.dao.findById(taskLog.getTaskId());
	            	//添加关键结果
	            	List<KeyResultsTask> vocationKeyResultList = KeyResultsTask.dao.find("SELECT * FROM key_results_task WHERE task_type = ? AND task_id = ? ORDER BY id DESC", Const.taskType.VOCATION_TASK_TYPE, vocationTask.getId());
	            	vocationTask.put("keyResultList", vocationKeyResultList);
	            	taskLog.put("task", vocationTask);
	                break;
	            case Const.taskType.EXAMPLE_ACTIVITY_TASK_TYPE://重要他人
	            	ExampleActivityTask exampleActivityTask = ExampleActivityTask.dao.findById(taskLog.getTaskId());
	            	//添加关键结果
	            	List<KeyResultsTask> exampleKeyResultList = KeyResultsTask.dao.find("SELECT * FROM key_results_task WHERE task_type = ? AND task_id = ? ORDER BY id DESC", Const.taskType.EXAMPLE_ACTIVITY_TASK_TYPE, exampleActivityTask.getId());
	            	exampleActivityTask.put("keyResultList", exampleKeyResultList);
	            	taskLog.put("task", exampleActivityTask);
	                break;
	            default:
	            	break;
        	}
        }

        return Ret.ok().set("taskLogPage", taskLogPage);
    }
    
    
    
    
    

}
